package seqlist.oj;

/**
 * @Author: yuisama
 * @Date: 2019/12/11 15:34
 * @Description:删除链表倒数第K个节点
 * https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/
 */
public class Solution19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if (head == null || head.next == null) {
            return null;
        }
        ListNode first = head,sec = head;
        int count = 0;
        while (first != null) {
            first = first.next;
            count ++;
            if (count >= n + 2) {
                sec = sec.next;
            }
        }
        if (count == n) {
            return head.next;
        }else {
            // 此时sec为前驱
            sec.next = sec.next.next;
            return head;
        }
    }
}
